package czx.wt.util.validate;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * @Description: 参数验证工具类
 * @Author: ChenZhiXiang
 * @CreateDate: 2018/12/18 10:02
 * @Version: 1.0
 */
public class ValidateUtil {

    /**
     * @描述：校验email格式
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern P_EMAIL =
            Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
    public static boolean isEmail(String str) {
        return str != null && P_EMAIL.matcher(str).matches();
    }

    /**
     * @描述：用户名正则，4到16位（字母，数字，下划线，减号）
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern USERNAME =
            Pattern.compile("^[a-zA-Z0-9_-]{4,16}$");
    public static boolean isUserName(String str) {
        return str != null && USERNAME.matcher(str).matches();
    }

    /**
     * @描述：校验简单密码格式，长度6到20位
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern SIMPLE_PASSWORD =
            Pattern.compile("^[0-9_a-zA-Z]{6,20}$");

    public static boolean isSimplePassword(String str) {
        return StringUtils.isNotBlank(str) && SIMPLE_PASSWORD.matcher(str).matches();
    }

    /**
     *
     * @描述：密码校验：匹配小写字母、大写字母、数字、特殊符号的两种及两种以上【非中文】，长度8到16位,,且密码不能包含用户名
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern PASSWORD = Pattern
            .compile("^(?![A-Z]+$)(?![a-z]+$)(?!\\d+$)(?![\\W_]+$)\\S{8,16}$");
    public static boolean isPassword(String username,String str) {
        return str != null && PASSWORD.matcher(str).matches() && !str.contains(username);
    }

    /**
     *
     * @描述：校验是否为11位1开头手机号
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern P_MOBILEPHONE = Pattern.compile("^1\\d{10}$");
    public static boolean isMobilephone(String str) {
        return StringUtils.isNotBlank(str) && P_MOBILEPHONE.matcher(str).matches();
    }

    /**
     *
     * @描述：校验是否正负数字
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern P_NUMBER = Pattern.compile("^[-\\+]?[\\d]+$");
    public static boolean isNumber(String str) {
        return StringUtils.isNotBlank(str) && P_NUMBER.matcher(str).matches();
    }

    /**
     *
     * @描述：校验是否为4-12位正整数
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern QQ = Pattern.compile("[1-9][0-9]{4,12}");
    public static boolean isQQ(String str) {
        return StringUtils.isNotBlank(str) && QQ.matcher(str).matches();
    }

    /**
     *
     * @描述：校验是否为6位数字验证码
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern CODE = Pattern.compile("[0-9]{6}$");
    public static boolean isCode(String str) {
        return StringUtils.isNotBlank(str) && CODE.matcher(str).matches();
    }

    /**
     *
     * @描述：校验是否为4位随机正整数和字母
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern PICCODE = Pattern.compile("\\w{4}$");
    public static boolean isPicCode(String str) {
        return str != null && PICCODE.matcher(str).matches();
    }

    /**
     *
     * @描述：校验是否为正负数（包含小数、整数）
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern P_DOUBLE = Pattern.compile("");
    public static boolean isDouble(String str) {
        return str != null && P_DOUBLE.matcher(str).matches();
    }

    /**
     *
     * @描述：校验是否为中文汉字,如果有其他非中文字符，false
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern P_CHINESE = Pattern
            .compile("^[\u0391-\uFFE5]+$");
    public static boolean isChinese(String str) {
        return str != null && P_CHINESE.matcher(str).matches();
    }

    /**
     *
     * @描述：校验是否包含中文,包含即为true
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern P_CHINESE_A = Pattern
            .compile("[\u0391-\uFFE5]");
    public static boolean hasChinese(String str) {
        return str != null && P_CHINESE_A.matcher(str).find();
    }

    /**
     *
     * @描述：校验是否6位数字短信验证码
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern MESCODE = Pattern.compile("^[0-9]{6}$");
    public static boolean isMessageCode(String str) {
        return str != null && MESCODE.matcher(str).matches();
    }

    /**
     * @描述：金额校验，正整数、小数点后两位
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern MONEY = Pattern
            .compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
    public static boolean isMoney(String str) {
        return str != null && MONEY.matcher(str).matches();
    }

    /**
     * @描述：身份证号校验
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern IDCARD= Pattern
            .compile("^\\d{15}|\\d{18}$");
    public static boolean isIdCard(String str) {
        return str != null && IDCARD.matcher(str).matches();
    }

    /**
     * @描述：年龄规则 1-120之间
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern AGE_PATTERN= Pattern
            .compile("^(?:[1-9][0-9]?|1[01][0-9]|120)$");
    public static boolean isAge(String str) {
        return str != null && AGE_PATTERN.matcher(str).matches();
    }

    /**
     * @描述：URL规则，http、www、ftp
     * @创建人：chenzhixiang
     * @param str
     * @return
     */
    private static final Pattern URL_PATTERN = Pattern
            .compile("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");
    public static boolean isUrl(String str) {
        return str != null && URL_PATTERN.matcher(str).matches();
    }
   /* public static void main(String[] args) {
        String email = "127227@qq.com";
        System.out.println("email----------"+isEmail(email));

        String password = "qwAAAAAA";
        System.out.println("password--------"+isPassword(password));

    }*/
}
